import Vue from 'vue'
import Vuex from 'vuex'
import deepClone from '../uview-ui/libs/function/deepClone.js'
Vue.use(Vuex)

let lifeData = {};

try{
	// 尝试获取本地是否存在lifeData变量，第一次启动APP时是不存在的
	lifeData = uni.getStorageSync('lifeData');
}catch(e){
	
}

// 需要永久存储，且下次APP启动需要取出的，在state中的变量名
let saveStateKeys = ['token','vuex_user','com_area','com_housetype','com_decorationtype','com_propertytype','com_orientation','com_area','com_address','com_price','vuex_interestrate','message_list'];

// 保存变量到本地存储中
const saveLifeData = function(key, value){
	// 判断变量名是否在需要存储的数组中
	if(saveStateKeys.indexOf(key) != -1) {
		// 获取本地存储的lifeData对象，将变量添加到对象中
		let tmp = uni.getStorageSync('lifeData');
		// 第一次打开APP，不存在lifeData变量，故放一个{}空对象
		tmp = tmp ? tmp : {};
		tmp[key] = value;
		// 执行这一步后，所有需要存储的变量，都挂载在本地的lifeData对象中
		uni.setStorageSync('lifeData', tmp);
	}
}
const store = new Vuex.Store({
	state: {
		token: lifeData.token ? lifeData.token : '',
		// 配置信息
		com_features: lifeData.com_features ? lifeData.com_features : '',
		com_housetype: lifeData.com_housetype ? lifeData.com_housetype : '',
		com_decorationtype: lifeData.com_decorationtype ? lifeData.com_decorationtype : '',
		com_propertytype: lifeData.com_propertytype ? lifeData.com_propertytype : '',
		com_orientation: lifeData.com_orientation ? lifeData.com_orientation : '',
		com_area: lifeData.com_area ? lifeData.com_area : '',
		com_address: lifeData.com_address ? lifeData.com_address : '',
		com_price: lifeData.com_price ? lifeData.com_price : '',
		vuex_houseMore:'',//房子详细详细，查看更多使用
		vuex_user:lifeData.vuex_user ? lifeData.vuex_user : '',
		vuex_interestrate:lifeData.vuex_interestrate ? lifeData.vuex_interestrate : '',//利率计算
		message_list:lifeData.message_list ? lifeData.message_list : {}//未读消息
	},
	mutations: {
		$uStore(state, payload) {
			// 判断是否多层级调用，state中为对象存在的情况，诸如user.info.score = 1
			let nameArr = payload.name.split('.');
			let saveKey = '';
			let len = nameArr.length;
			if(len >= 2) {
				let obj = state[nameArr[0]];
				for(let i = 1; i < len - 1; i ++) {
					obj = obj[nameArr[i]];
				}
				obj[nameArr[len - 1]] = payload.value;
				saveKey = nameArr[0];
			} else {
				// 单层级变量，在state就是一个普通变量的情况
				state[payload.name] = payload.value;
				saveKey = payload.name;
			}
			// 保存变量到本地，见顶部函数定义
			saveLifeData(saveKey, state[saveKey])
		},
		setHouseMore(state,payload){
			state.vuex_houseMore = payload
		},
		add_message_list(state,payload){
			let message = deepClone(state.message_list)
					message[payload.id] = payload.hide ? null : deepClone(payload)
			state.message_list = message
			// let flag = false
			// state.message_list.forEach((item,index)=>{
			// 	if(item.id === payload.id){
			// 		flag = true
			// 		state.message_list.splice(index,1,payload)
			// 	}
			// })
			// flag || state.message_list.push(payload)
			saveLifeData('message_list',message)
		}
	},
	actions:{
		initConfigForVuex({commit},payload){
			payload.$u.api.getAddressConfig().then(res=>commit('$uStore',{name:'com_address',value:res[0]}))
			payload.$u.api.getPriceConfig().then(res=>commit('$uStore',{name:'com_price',value:res}))
			payload.$u.api.com_features().then(res=>commit('$uStore',{name:'com_features',value:res}))
			payload.$u.api.com_housetype().then(res=>commit('$uStore',{name:'com_housetype',value:res}))
			payload.$u.api.com_decorationtype().then(res=>commit('$uStore',{name:'com_decorationtype',value:res}))
			payload.$u.api.com_propertytype().then(res=>commit('$uStore',{name:'com_propertytype',value:res}))
			payload.$u.api.com_orientation().then(res=>commit('$uStore',{name:'com_orientation',value:res}))
			payload.$u.api.com_area().then(res=>commit('$uStore',{name:'com_area',value:res}))
			payload.$u.api.interestrate().then(res=>commit('$uStore',{name:'vuex_interestrate',value:res}))
		},
		async getUserInfo({commit},payload){
			const res = await payload.$u.api.getUserInfo()
			commit('$uStore',{name:'vuex_user',value:res})
			return res
		}
	}
})

export default store
